package cn.qin.DAO.impl;

import java.util.List;


import net.sf.json.JSONArray;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.json.JSONException;

import cn.qin.DAO.newsOperateDAO;
import cn.qin.Models.NEWS;
import cn.qin.Models.admin;

public class newsOperateDAOimpl implements newsOperateDAO {
	private static SessionFactory  sessionFactory;
	@SuppressWarnings("deprecation")
	public void saveNews(NEWS news) {	
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		news.setId(news.getId());
		news.setTitle(news.getTitle());
		news.setContent(news.getContent());
		news.setDate(news.getDate());
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(news);
		session.getTransaction().commit();
		sessionFactory.close();	
	}
	
	@SuppressWarnings("deprecation")
	public void delNews(int id) {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery(" delete FROM NEWS WHERE id="+id);
		q.executeUpdate();
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
	}
	
	@SuppressWarnings("deprecation")
	public JSONArray showNews(String c_id) throws JSONException {
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			String hql="select id,title,p_time FROM NEWS where c_id='"+c_id+"' order by id desc";
			Query q=session.createQuery(hql);
			List list = q.list();
			JSONArray json = JSONArray.fromObject(list);
			session.getTransaction().commit();
			sessionFactory.close();
			return json;
		}
	@SuppressWarnings("deprecation")
	public boolean login(admin admin){
		boolean b=false;
		SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();

		Query q = session.createQuery("FROM admin WHERE name=?");
		q.setString(0,admin.getName());
		
		@SuppressWarnings("unchecked")
		List<admin> list =(List<admin>)q.list();
		
		if((list!=null)&&(list.size()>0)){
			String admin_name =list.get(0).getName();
			String admin_password=list.get(0).getPassword();
			if(admin_name.equals(admin.getName())&&admin_password.equals(admin.getPassword())){
				b=true;
				
			}else{
				b=false;
				}
		}
		else 
		{
		b=false;
		}
		
		if(b)
			return true;
		else 
			return false;

			
		//session.close();
		//sessionFactory.close();	
	}

	@SuppressWarnings("deprecation")
	public JSONArray showOneNews(String id) {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery("FROM NEWS where id=?");
		q.setString(0, id);
		List list = q.list();
		JSONArray json = JSONArray.fromObject(list);
		session.getTransaction().commit();
		sessionFactory.close();
		return json;
	}

	@SuppressWarnings("deprecation")
	public JSONArray showNewsByCid(int count, String c_id) throws JSONException {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql="select id,title,p_time FROM NEWS where c_id='"+c_id+"' order by id desc";
		Query q = session.createQuery(hql).setMaxResults(count);
		List list = q.list();
		JSONArray json = JSONArray.fromObject(list);
		session.getTransaction().commit();
		sessionFactory.close();
		return json;
	}

	public JSONArray showHot() {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		String hql="select id,title,imgurl from NEWS where hot='1' order by id desc";
		
		Query q = session.createQuery(hql).setMaxResults(4);
		List list = q.list();
		JSONArray json = JSONArray.fromObject(list);
		session.getTransaction().commit();
		sessionFactory.close();
		return json;
	}

	@SuppressWarnings("deprecation")
	public void modifyPassword(String password,String username) {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();

		Query q = session.createQuery("update admin set password=? where name=?");
		q.setString(0, password);
		q.setString(1, username);
		q.executeUpdate();
		tx.commit();
		session.close();
		
	}

	@Override
	public void editNews(int id,String title,String c_id,String content,int hot,String imgurl) {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Query q =session.createQuery("update NEWS set title=?,content=?,c_id=?,hot=?,imgurl=? where id="+id);
		q.setString(0, title);
		q.setString(1, content);
		q.setString(2, c_id);
		q.setInteger(3, hot);
		q.setString(4, imgurl);
		//q.setInteger(5, id);
		q.executeUpdate();
		tx.commit();
		session.close();
	}
	}
	